public class singleLinkedList {

    class ListNode {
        int val;
        ListNode next;

        public ListNode(int x) {
            this.val = x;
        }
    }
    ListNode head;
    public void addFirst(int data){
        ListNode newNode = new ListNode(data);
        if(head == null){
            head = newNode;
        }else {
            newNode.next = head;
            head = newNode;
        }
    }
    //尾插法
    public void addLast(int data){
        ListNode newNode = new ListNode(data);
        ListNode temp = head;
        while(temp.next != null){
            temp = temp.next;
        }
        if(head == null){
            head = newNode;
        }else {
            temp.next = newNode;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode newNode = new ListNode(data);
        ListNode temp = head;
        for(int i=0;i<index;i++){
            temp = temp.next;
        }
        newNode.next = temp.next;
        temp.next =newNode;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(head == null){
            return false;
        }else {
            ListNode temp = head;
            while(temp != null){
                if(temp.val == key){
                    return true;
                }
                temp = temp.next;
            }
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head.next == null&&head.val == key){
            head = head.next;
            return;
        }
        if(head == null){
            return;
        }
        if(head.val == key){
            head = head.next;
        }
        ListNode temp = head;
        while(temp.next != null){
            if(temp.next.val == key){
                temp.next = temp.next.next;
                return;
            }else {
                temp = temp.next;
            }
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head.next == null&&head.val == key){
            head = head.next;
        }
        if(head == null){
            return;
        }
        if(head.val == key){
            head = head.next;
        }
        ListNode temp = head;
        while(temp.next != null){
            if(temp.next.val == key){
                temp.next = temp.next.next;
            }else {
                temp = temp.next;
            }
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode temp = head;
        int count = 0;
        while(temp != null){
            count++;
            temp = temp.next;
        }
        return count;
    }
    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val);
            cur = cur.next;
        }
    }
    public void clear(){
        head = null;
    }
}
